{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "91af3eba",
   "metadata": {},
   "outputs": [],
   "source": [
    "%use fuel(2.3.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d86fa867",
   "metadata": {},
   "outputs": [],
   "source": [
    "val envs = java.io.File(\"../../.env\")\n",
    "    .readLines()\n",
    "    .map {\n",
    "        it.split(\"=\")[0] to it.split(\"=\")[1].trim('\"')\n",
    "    }.toMap()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "65e62477",
   "metadata": {},
   "outputs": [],
   "source": [
    "val session = envs.get(\"AOC_SESSION\")\n",
    "val year = 2021\n",
    "val day = 17"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a2bb91d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun getInput(year: Int, day: Int, session: String): String {\n",
    "    val (_, _, result) = \"https://adventofcode.com/$year/day/$day/input\"\n",
    "    .httpGet()\n",
    "    .header(\"cookie\" to \"session=$session\")\n",
    "    .responseString()\n",
    "        \n",
    "    return result.get().trim()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f0917bbf",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun submitAnswer(year: Int, day: Int, session: String, level: Int, answer: String): String {\n",
    "    val (_, _, result) = Fuel\n",
    "    .post(\n",
    "        \"https://adventofcode.com/$year/day/$day/answer\", \n",
    "        parameters = listOf(\"level\" to level, \"answer\" to answer))\n",
    "    .header(\"cookie\" to \"session=$session\")\n",
    "    .responseString()\n",
    "        \n",
    "    return result.get()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "dd0e5a59",
   "metadata": {},
   "outputs": [],
   "source": [
    "val sample = \"\"\"target area: x=20..30, y=-10..-5\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "83fc6186",
   "metadata": {},
   "outputs": [],
   "source": [
    "val input = getInput(year, day, session)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c294602f-9214-4ce7-a1fe-9687580dccd7",
   "metadata": {},
   "outputs": [],
   "source": [
    "val regex = Regex(\"\"\"target area: x=(-?\\d+)..(-?\\d+), y=(-?\\d+)..(-?\\d+)\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a8bcc744-fad7-4cb7-b306-1d1223376484",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun solve(l: Int, r: Int, d: Int, u: Int): Pair<Int, Int> {\n",
    "    var validPath = 0\n",
    "    var highest = 0\n",
    "    val vxMin = l.coerceAtMost(0)\n",
    "    val vxMax = r.coerceAtLeast(0)\n",
    "    val vyMin = d.coerceAtMost(0)\n",
    "    val vyMax = abs(d).coerceAtLeast(abs(u))\n",
    "    for (vx0 in vxMin..vxMax) {\n",
    "        for (vy0 in vyMin..vyMax) {\n",
    "            var vx = vx0\n",
    "            var vy = vy0\n",
    "            var x = 0\n",
    "            var y = 0\n",
    "            var collision = false\n",
    "            while (true) {\n",
    "                x += vx\n",
    "                y += vy\n",
    "                if (x >= l && x <= r && y >= d && y <= u)\n",
    "                    collision = true\n",
    "                if (vx > 0)\n",
    "                    vx--\n",
    "                else if (vx < 0)\n",
    "                    vx++\n",
    "                vy--\n",
    "                \n",
    "                if ((y < d && vy <= 0) || (x < l && vx <= 0) || (x > r && vx >= 0))\n",
    "                    break\n",
    "            }\n",
    "            \n",
    "            if (collision) {\n",
    "                validPath++\n",
    "                highest = highest.coerceAtLeast(vy0 * (vy0 + 1) / 2)\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "    \n",
    "    return Pair(validPath, highest)\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c542c6ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun partOne(input: String): String {\n",
    "    val (l, r, d, u) = regex\n",
    "        .matchEntire(input)!!\n",
    "        .groupValues.slice(1 until 5)\n",
    "        .map(String::toInt)\n",
    "    \n",
    "    return solve(l, r, d, u).second.toString()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "cc01ce3c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "45"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "partOne(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a94d0dcb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10296"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val partOneAns = partOne(input)\n",
    "partOneAns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e0d4935",
   "metadata": {},
   "outputs": [],
   "source": [
    "submitAnswer(year, day, session, 1, partOneAns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "9e7a88eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun partTwo(input: String): String {\n",
    "    val (l, r, d, u) = regex\n",
    "        .matchEntire(input)!!\n",
    "        .groupValues.slice(1 until 5)\n",
    "        .map(String::toInt)\n",
    "    \n",
    "    return solve(l, r, d, u).first.toString()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "261894c7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "112"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "partTwo(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "11f62ebc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2371"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val partTwoAns = partTwo(input)\n",
    "partTwoAns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "899192ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "submitAnswer(year, day, session, 2, partTwoAns)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Kotlin",
   "language": "kotlin",
   "name": "kotlin"
  },
  "language_info": {
   "codemirror_mode": "text/x-kotlin",
   "file_extension": ".kt",
   "mimetype": "text/x-kotlin",
   "name": "kotlin",
   "nbconvert_exporter": "",
   "pygments_lexer": "kotlin",
   "version": "1.6.20-dev-5432"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
